<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('moderators_mod');
    }

    /**
     *   
     *    
     */
    public function index() {
        $data['menu_page'] = "home"; // Koja je stranica. current menu item
        // Definisi koji se view ucitava
        $data['contents'] = 'home/home';
        $data['conversations'] = $this->moderators_mod->getConversations();
        $this->load->view('main', $data);
    }

    public function get_Conversation() {
        try {
            $fromId = $this->input->post('userFrom');
            $toId = $this->input->post('userTo');
            if ($this->moderators_mod->modActivityExists($fromId, $toId)) {
            $status = $this->moderators_mod->takeModActivity($fromId, $toId);
            }
            else{
            $status = $this->moderators_mod->startModActivity($fromId, $toId);
            }
            if ($status != "Busy") {
                $this->moderators_mod->setFakeUserOnline($toId);
                $this->moderators_mod->setViewProfile($fromId, $toId);
                $data['user_from']=$this->moderators_mod->getUserData($fromId);
                $data['user_to']=$this->moderators_mod->getUserData($toId);
                $data['messages'] = $this->moderators_mod->getConversation($fromId, $toId);
                //$data['pokes'] = $this->moderators_mod->getPokes($fromId, $toId);
                $html = $this->load->view('home/messages_list', $data, true);
                $response = array(
                    'ind' => 1,
                    'msg' => $status . " conversation",
                    'html' => $html
                );
            }
            else {
                
                $response = array(
                'ind' => 0,
                'msg' => 'Conversation already taken',
                'html' => ' '
                );
            }
            //$data['user_from']=$this->moderators_mod->getUserData($fromId);
            //$data['user_to']=$this->moderators_mod->getUserData($toId);
//            $data['zip_code']=$user_data->zip_code;
//            $data['coins']=$user_data->coins;
//            $data['description']=$user_data->description;
//            $data['id']=$user_data->id;
//            $data['age'] = $user_data->age;
            
           /* $html = $this->load->view('home/messages_list', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => "",
                'html' => $html
            );*/

        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }

    public function send_mod_message() {

        try {
          $fromId = $this->input->post('userFrom');
          $toId = $this->input->post('userTo');
          $message = $this->input->post('message');
          $this->moderators_mod->send_mod_message($fromId, $toId, $message);
//          $user_data = $this->moderators_mod->getUserData($fromId);
//          $data['zip_code'] = $user_data->zip_code;
//          $data['coins'] = $user_data->coins;
//          $data['description'] = $user_data->description;
//          $data['id'] = $user_data->id;
//          $data['age'] = $user_data->age;
          
           $data['user_from']=$this->moderators_mod->getUserData($fromId);
           $data['user_to']=$this->moderators_mod->getUserData($toId);
          
            if ($fromId == NULL || $fromId == "" || $toId == NULL || $toId == "")
              $data['messages'] = NULL;
            else
              $data['messages'] = $this->moderators_mod->getConversation($fromId, $toId);
            $html = $this->load->view('home/messages_list', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => " ",
                'html' => $html
            );
      
      } catch (Exception $e) {
        $response = array(
            'ind' => 0,
            'msg' => 'Following errors occured: <br />' . $e->getMessage(),
            'html' => ''
        );
      }
      echo json_encode($response);
    }

    public function refresh_conversations() {
        try {
            $data['conversations'] = $this->moderators_mod->getConversations();
            $html = $this->load->view('home/conversation_list', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => "",
                'html' => $html
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }
    public function start_conversation (){
           try {
            $data['fake_users'] = $this->moderators_mod->getFakeUsers();
            $data['users'] = $this->moderators_mod->getUsers();
            $html = $this->load->view('home/dialog_start_conversation', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => "",
                'html' => $html
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }
   public function submit_conversation (){
       $conversation = $this->input->post('conversation');
        $fromId=$conversation['from_id'];
        $toId=$conversation['to_id'];
        $message=$conversation['message'];
        try {
            $this->moderators_mod->send_mod_message($fromId, $toId, $message);
            
            $response = array(
                'ind' => 1,
                'msg' => "",
                
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                
            );
        }
        echo json_encode($response);
   }
    public function save_description (){
       $description = $this->input->post('description');
       $fromId = $this->input->post('fromId');
        try {
            $this->moderators_mod->saveDescription($fromId, $description);
            
            $response = array(
                'ind' => 1,
                'msg' => lang('term_saved'),
                
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                
            );
        }
        echo json_encode($response);
   }
}

/* End of file home.php */
/* Location: ./moderation/controllers/home.php */